package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.flyco.roundview.RoundTextView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.adapter.OrderInfoProductListAdapter;
import com.gensdai.leliang.common.bean.ApplyOrderInfoBean;
import com.gensdai.leliang.common.bean.CommentInfoBean;
import com.gensdai.leliang.view.NoScrollListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 售后列表
 * Created by LT on 2017/6/15.
 */

public class RefundListAdapterRec extends DelegateAdapter.Adapter<RefundListAdapterRec.MainViewHolder> {


    private Context mContext;

    private LayoutHelper mLayoutHelper;

    OnInfoItemClick click;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    List<ApplyOrderInfoBean> blist;

    public RefundListAdapterRec(Context context, LayoutHelper layoutHelper, List<ApplyOrderInfoBean> list) {
        this(context, layoutHelper, list, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public RefundListAdapterRec(Context context, LayoutHelper layoutHelper, List<ApplyOrderInfoBean> list, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.blist = list;
        this.mLayoutParams = layoutParams;
    }

    public void setOnInfoItemClick(OnInfoItemClick click){
        this.click = click;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_refund_comment, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, final int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));

        final ApplyOrderInfoBean info = blist.get(position);

        OrderInfoProductListAdapter adapter = new OrderInfoProductListAdapter(mContext,false);
        holder.plist.setAdapter(adapter);
        adapter.initAdd(info.getProductInfo());

        holder.refundInfo.setText("退款金额:￥"+info.getRefundPrice() + "(" + info.getInfo() + ")");
        if(7 == info.getOrderState()){
            holder.refundlable.setText("退款成功");
        }else if(8 == info.getOrderState()){
            holder.refundlable.setText("退款中");
        }

        holder.infoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(click != null){
                    click.onClick(position,info.getOrderId());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return blist.size();
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.plist)
        NoScrollListView plist;
        @BindView(R.id.refundlable)
        TextView refundlable;
        @BindView(R.id.refundInfo)
        TextView refundInfo;
        @BindView(R.id.infoBtn)
        RoundTextView infoBtn;

        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }

    public interface OnInfoItemClick{
        void onClick(int position,String orderid);
    }

}
